﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FutureTreeController : MonoBehaviour
{
    GameBoard gameBoard;
    Animator animator;
    public bool burn;
    float life;

    private void Awake()
    {
        gameBoard = GameObject.Find("Board").GetComponent<GameBoard>();
        animator = gameObject.GetComponent<Animator>();
        life = 10f;
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (burn && life > 0) {
            life -= Time.deltaTime;
        }
        if (life <= 0) {
            gameBoard.removeBlock(transform.localPosition);
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        GameObject target = collision.gameObject;
        if (target.layer == 10 || target.layer == 8) {
            target.SendMessage("EnterTree", burn);
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        GameObject target = collision.gameObject;
        if (target.layer == 10 || target.layer == 8)
        {
            target.SendMessage("ExitTree");
        }
    }

    private void GetHurt(int hurt)
    {
        //Debug.Log("hurt");
        //if (hurt == 50)
        //{
        //    burn = true;
        //    animator.SetBool("Burn", true);
        //    gameObject.GetComponent<Collider2D>().enabled = false;
        //    gameObject.GetComponent<Collider2D>().enabled = true;
        //}
    }

    public void StartBurn() {
        burn = true;
        animator.SetBool("Burn", true);
        gameObject.GetComponent<Collider2D>().enabled = false;
        gameObject.GetComponent<Collider2D>().enabled = true;
    }
}
